package com.avblog.console.ui;

import java.util.ArrayList;
import java.util.List;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Component;
import java.beans.PropertyVetoException;
import java.net.URL;
import java.io.File;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
//import javax.swing.UIManager;
import javax.swing.DesktopManager;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.JTabbedPane;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JFileChooser;
import javax.swing.JEditorPane;
import javax.swing.filechooser.FileNameExtensionFilter;

import javax.swing.text.*;

import com.avblog.console.component.ToolBarButton;
import com.avblog.console.convert.Converter;


public class AppFrame extends JFrame implements IMainAppFrame{

	private JDesktopPane desktopPane;
	private DesktopManager desktopManager;
	private JMenuBar menuBar;
	private JMenu mnFile, mnTool, mnHelp;
	private JMenuItem mniQuit, mniConverter, mniArticleMgmt, mniHelp;
	
	private JToolBar toolBar;
	
	private List<IInternalFrameListener> listeners = new ArrayList<IInternalFrameListener>();

	public AppFrame(){
		super("Nautilus");
		
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		
		desktopPane = new JDesktopPane();
		desktopPane.setOpaque(false);
		initComponent();
		
		toolBar = new JToolBar("Toolbar", JToolBar.HORIZONTAL);
		c.add(toolBar, BorderLayout.NORTH);
		
		//setContentPane(desktopPane);
		c.add(desktopPane, BorderLayout.CENTER);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	private void initComponent(){
		int inset = 10;
		Dimension dimen = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds(inset, inset, dimen.width - inset*2, dimen.height - inset*2);
		
		//Menubar
		menuBar = new JMenuBar();
		mnFile = new JMenu("File");
		mnTool = new JMenu("Tools");
		mnHelp = new JMenu("Help");
		
		mniQuit = new JMenuItem("Quit");
		mniQuit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				int optionChosen = JOptionPane.showConfirmDialog(AppFrame.this, "Do you really want to quit?", "Quit Program", JOptionPane.YES_NO_OPTION);
				if(optionChosen == JOptionPane.YES_OPTION)
					System.exit(0);
			}
		});
		mnFile.add(mniQuit);
		
		mniConverter = new JMenuItem("Code to HTML");
		mniConverter.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				ConvertPane convertPane = new ConvertPane();
				desktopPane.add(convertPane);
				addInternalFrameListener(convertPane);
				convertPane.setParent(AppFrame.this);
				try{
					convertPane.setVisible(true);
					convertPane.setSelected(true);
					convertPane.setMaximum(true);
				}catch(PropertyVetoException e1){
					e1.printStackTrace();
				}
			}
		});
		mnTool.add(mniConverter);
		
		mniArticleMgmt = new JMenuItem("Articles");
		mniArticleMgmt.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				ArticleMgmPane articlesPane = new ArticleMgmPane();
				desktopPane.add(articlesPane);
				addInternalFrameListener(articlesPane);
				articlesPane.setParent(AppFrame.this);
				try{
					articlesPane.setVisible(true);
					articlesPane.setSelected(true);
					articlesPane.setMaximum(true);
				}catch(PropertyVetoException e1){
					e1.printStackTrace();
				}
			}
		});
		mnTool.add(mniArticleMgmt);
		
		mniHelp = new JMenuItem("Help");
		mniHelp.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				JOptionPane.showMessageDialog(AppFrame.this, "Copyright @Dao Anh Vu 2014", "AVBLOG CONSOLE", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		mnHelp.add(mniHelp);
		
		menuBar.add(mnFile);
		menuBar.add(mnTool);
		menuBar.add(mnHelp);
		
		this.setJMenuBar(menuBar);
	}
	
	public void addInternalFrameListener(IInternalFrameListener listener){
		listeners.add(listener);
	}
	
	public void removeInternalFrameListener(IInternalFrameListener listener){
		listeners.remove(listener);
	}
	
	public void addButtonToolBar(ToolBarButton[] buttons){
		//toolBar.addSeparator();
		for(int i=0; i<buttons.length; i++)
			toolBar.add(buttons[i]);
		toolBar.revalidate();
		toolBar.repaint();
	}
	
	public void removeButtonToolBar(ToolBarButton[] buttons){
		for(int i=0; i<buttons.length; i++)
			toolBar.remove(buttons[i]);
	}
}
